package userInterface;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class ConsoleFrame extends JFrame{
	public static JTextArea text;
	public ConsoleFrame() {
		super("Console");
		setBounds(100,100,500,375);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setLocationRelativeTo(EnvironmentVariables.mainFrame);
		text = new JTextArea();
		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		text.setColumns(EnvironmentVariables.consoleColumns);
		JScrollPane textWithScrollBar = new JScrollPane(text);
		textWithScrollBar.setHorizontalScrollBar(textWithScrollBar.createHorizontalScrollBar());
		textWithScrollBar.setVerticalScrollBar(textWithScrollBar.createVerticalScrollBar());
		textWithScrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		textWithScrollBar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		text.setRows(EnvironmentVariables.consoleRows);
		text.setEditable(false);
		
		getContentPane().add(textWithScrollBar,BorderLayout.CENTER);
		setVisible(true);
	}
    public static void init() {
        OutputStream textAreaStream = new OutputStream() {
        	public void write(int b) throws IOException {
        		text.append(String.valueOf((char)b));
        		text.setCaretPosition(text.getText().length());
        	}
        	public void write(byte b[]) throws IOException {
        		text.append(new String(b));
        		text.setCaretPosition(text.getText().length());
        	}
        	public void write(byte b[],int off,int len) throws IOException {
        		text.append(new String(b,off,len));
        		text.setCaretPosition(text.getText().length());
        	}
        };
        PrintStream myOut = new PrintStream(textAreaStream);
        System.setOut(myOut);
        //System.setErr(myOut);
    }

}